//
//  TTIPAPurchaseManger.m
//  togetherPlay
//
//  Created by coder on 2019/1/15.
//  Copyright © 2019 stoneobs.qq.com. All rights reserved.
//

#import "STTools_UserIPAPurchaseManger.h"
@interface STTools_UserIPAPurchaseManger()
@property(nonatomic, strong) NSString                     *userId;/**< 当前用户 */
@end
@implementation STTools_UserIPAPurchaseManger
+ (STTools_UserIPAPurchaseManger*)manger{
    static STTools_UserIPAPurchaseManger * defult = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        defult = [STTools_UserIPAPurchaseManger new];
    });
    return defult;
}
- (void)stoneobs_fun_st_beginMonitorWithUserID:(NSString *)userId{
    self.userId = userId;
    [STTools_IPAPurchaseManger.manger stoneobs_fun_st_beginMonitorWithUserID:userId];
    [STTools_IPAPurchaseManger.manger setUnusualHandle:^(NSArray<STTools_IPAPurchaseModel *> * _Nonnull orderArray) {
        [self autoDealUnusualArray:orderArray.copy];
    }];
}
- (void)autoDealUnusualArray:(NSArray*)array{
    for (STTools_IPAPurchaseModel * model in array) {
                          if ([model.onwer_id isEqualToString:self.userId]) {
            //解决异常订单
            [self sendInputMRequestWithApple_receipt:model.receiptString handle:^(BOOL success, NSString * _Nonnull alartHandle) {
                
            }];
        }
    }
}
#pragma mark --NetWork Method
- (void)sendInputMRequestWithApple_receipt:(NSString*)apple_receipt handle:(nonnull void (^)(BOOL, NSString * _Nonnull))handle{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * paramDic = [NSMutableDictionary new];
//    NSString * userID = [STTools_UserManger defult].stoneobs_fun_get_loginedUser.uid.description;
//    NSString * token = STTools_UserManger.defult.stoneobs_fun_get_loginedUser.token;
//    if (userID.length) {
//        [paramDic setObject:userID forKey:@"uid"];
//    }
//    if (token.length) {
//        [paramDic setObject:token forKey:@"token"];
//    }
    if (apple_receipt.length) {
        [paramDic setObject:apple_receipt forKey:@"apple_receipt"];
    }
    NSString * url = [NSString stringWithFormat:@"%@%@",RELEASE_SERVER_URL_HEADER,@"api/orderCheck"];
    DDLogInfo(@"url:%@",url);
    DDLogInfo(@"param:%@",paramDic);
    [[STTools_NetWrokManger defaultClient] stoneobs_fun_requestWithPath:url
                                              method:STHttpRequestType_stoneobs_enumPost
                                          parameters:paramDic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showSuccessWithStatus:@"充值成功"];
                                                 if (handle) {
                                                     handle(YES,@"");
                                                 }
                                                 [[STTools_IPAPurchaseManger manger] stoneobs_fun_st_makeOrderCompleteWithReceiptString:apple_receipt];
                                                 // DDLogInfo(@"json = \n%@",modelDic.mj_JSONString);
                                                 DDLogInfo(@"%@请求成功:resp\n%@",url,responseObject);
                                                 [STTools_IPAPurchaseManger.manger stoneobs_fun_st_makeOrderCompleteWithReceiptString:apple_receipt];
                                                 if (handle) {
                                                     handle(YES,@"");
                                                 }
                                                 
                                             } failure:^(NSString *stateCode, STTools_Error *error,NSError *originError) {
                                                 [SVProgressHUD dismiss];
                                                 if (handle) {
                                                     handle(NO,error.desc);
                                                 }
                                                 [TMUtils.stoneobs_fun_get_currentTopController stoneobs_fun_st_showAlertTitle:@"检测到您上次苹果支付结果异常，请联系客服处理" message:@"" leftTitle:@"" rightTitle:@"确认" block:^(NSString *name) {
                                                     
                                                 }];
//                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                                 DDLogError(@"\n请求失败:\nurl:%@\nparam:%@\n失败原因:%@\n错误码:%ld",
                                                            url,paramDic,error.desc,error.code);
                                             }];
}
@end
